package com.kaishengit.util.db;

import java.util.ArrayList;
import java.util.List;

/**
 * ������--��ҳ
 * @author Administrator
 *
 * @param <T>
 */
public class Page<T> {
	private int pageNum;
	private int pageSize = 3;
	private int totalCount;
	private List<T> list;
	
	public Page() {}
	
	public Page(int totalCount,int pageNum) {
		setTotalCount(totalCount);
		setPageNum(pageNum);
	}
	
	
	/**
	 * 获取当前页的起始行号
	 * @return
	 */
	public int getStart() {
		int start = (getPageNum() - 1) * getPageSize();
		return start;
	}
	
	/**
	 * 获取当前的页码
	 * @return
	 */
	public int getPageNum() {
		return pageNum;
	}
	/**
	 * 设置当前的页码
	 * @param pageNum
	 */
	public void setPageNum(int pageNum) {
		if(pageNum > getTotalPages()) {
			pageNum = getTotalPages();
		} else if(pageNum < 1) {
			pageNum = 1;
		}
		this.pageNum = pageNum;
	}
	
	/**
	 * 获取总页数
	 * @return
	 */
	public int getTotalPages() {
		int pages = getTotalCount() / getPageSize();
		if(getTotalCount() % getPageSize() != 0) {
			pages++;
		}
		if(pages == 0) {
			pages = 1;
		}
		return pages;
	}
	/**
	 * 获取每页显示的数量
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * 设置每页显示的数量
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public int getTotalCount() {
		return totalCount;
	}
	/**
	 * 设置总记录数
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	/**
	 * 获取当前页的结果集合
	 * @return
	 */
	public List<T> getList() {
		return list;
	}
	/**
	 * 设置当前页的结果集合
	 * @param list
	 */
	public void setList(List<T> list) {
		this.list = list;
	}
	
	/**
	 * 计算以当前页为中的页码集合
	 * @return
	 */
	public List<Integer> getSlider() {
		int count = 5;
		
		int halfSize = count / 2;
		int totalPage = getTotalPages();
 
		int startPageNo = Math.max(getPageNum() - halfSize, 1);
		int endPageNo = Math.min(startPageNo + count - 1, totalPage);
 
		if (endPageNo - startPageNo < count) {
			startPageNo = Math.max(endPageNo - count, 1);
		}
 
		List<Integer> result = new ArrayList<Integer>();
		for (int i = startPageNo; i <= endPageNo; i++) {
			result.add(i);
		}
		return result;
	}
}
